import {
  render,
  renderGlobalModal,
  screen,
  userEvent,
} from 'sentry-test/reactTestingLibrary';

import * as indicators from 'sentry/actionCreators/indicator';
import type {OrgAuthToken} from 'sentry/types/user';
import OrganizationAuthTokensNewAuthToken from 'sentry/views/settings/organizationAuthTokens/newAuthToken';

describe('OrganizationAuthTokensNewAuthToken', () => {
  const ENDPOINT = '/organizations/org-slug/org-auth-tokens/';

  it('can create token', async () => {
    render(<OrganizationAuthTokensNewAuthToken />);
    renderGlobalModal();

    const generatedToken: OrgAuthToken & {token: string} = {
      id: '1',
      name: 'My Token',
      token: 'sntrys_XXXXXXX',
      tokenLastCharacters: 'XXXX',
      dateCreated: new Date('2023-01-01T00:00:00.000Z'),
      scopes: ['org:read'],
    };

    const mock = MockApiClient.addMockResponse({
      url: ENDPOINT,
      method: 'POST',
      body: generatedToken,
    });

    expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();

    await userEvent.type(screen.getByLabelText('Name'), 'My Token');
    await userEvent.click(screen.getByRole('button', {name: 'Create Token'}));

    expect(await screen.findByLabelText('Generated token')).toHaveValue('sntrys_XXXXXXX');

    expect(mock).toHaveBeenCalledWith(
      ENDPOINT,
      expect.objectContaining({
        data: {name: 'My Token'},
      })
    );
  });

  it('handles API errors when creating token', async () => {
    jest.spyOn(indicators, 'addErrorMessage');

    render(<OrganizationAuthTokensNewAuthToken />);

    const mock = MockApiClient.addMockResponse({
      url: ENDPOINT,
      method: 'POST',
      body: {
        detail: 'Test API error occurred.',
      },
      statusCode: 400,
    });

    expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();

    await userEvent.type(screen.getByLabelText('Name'), 'My Token');
    await userEvent.click(screen.getByRole('button', {name: 'Create Token'}));

    expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();

    expect(indicators.addErrorMessage).toHaveBeenCalledWith(
      'Failed to create a new organization token.'
    );

    expect(mock).toHaveBeenCalledWith(
      ENDPOINT,
      expect.objectContaining({
        data: {name: 'My Token'},
      })
    );
  });

  it('handles missing_system_url_prefix API error when creating token', async () => {
    jest.spyOn(indicators, 'addErrorMessage');

    render(<OrganizationAuthTokensNewAuthToken />);

    const mock = MockApiClient.addMockResponse({
      url: ENDPOINT,
      method: 'POST',
      body: {
        detail: {message: 'test message', code: 'missing_system_url_prefix'},
      },
      statusCode: 400,
    });

    expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();

    await userEvent.type(screen.getByLabelText('Name'), 'My Token');
    await userEvent.click(screen.getByRole('button', {name: 'Create Token'}));

    expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();

    expect(indicators.addErrorMessage).toHaveBeenCalledWith(
      'You have to configure `system.url-prefix` in your Sentry instance in order to generate tokens.'
    );

    expect(mock).toHaveBeenCalledWith(
      ENDPOINT,
      expect.objectContaining({
        data: {name: 'My Token'},
      })
    );
  });
});
